package SecondeStage.Collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {
    public static void main(String[] args) {
        Collection collection = new ArrayList();
        collection.add(new Book("三国演义","罗贯中",10.1));
        collection.add(new Book("小李飞刀","古龙",10.1));
        collection.add(new Book("红楼梦","曹雪芹",10.1));
        Iterator iterator = collection.iterator();
        // 快捷键，快速生成 while： itit
        while (iterator.hasNext()){
            Object object = iterator.next();
            System.out.println(object);
        }
        // 当退出while循环后，iterator迭代器指向最后的元素
        // 此时如果再次执行 iterator.next() 会报错： NoSuchElementException
        // 如果希望再次遍历，需要重置迭代器
        System.out.println("--------------------");
        iterator = collection.iterator();
        while (iterator.hasNext()) {
            Object object = iterator.next();
            System.out.println(object);
        }


    }
}

class Book{
    private String name;
    private String author;
    private double price;

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", author='" + author + '\'' +
                ", price=" + price +
                '}';
    }
}